﻿using System;
using System.Collections.Generic;
using System.Text;
using YouHP.BLL;
using YouHP.Utility;
using YouHP.Model;
using System.Text.RegularExpressions;

namespace YouHP.Pages.Member
{
    public class BasicInfo : YouHP.Pages.BasePage
    {

        protected new UserInfo User = new UserInfo();
      

        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;
            

            PageTitle = "用户控制面板";

            if (UserData.Id == -1)
            {
                AddErrLine("你尚未登录");

                return;
            }


            if (RequestTools.IsPost()) HandlePost();
            else HandleGet();
        }


        void HandleGet()
        {

            User = UserBLL.Instance.GetUserInfo(UserData.Id);
        }


        void HandlePost()
        {

            if (RequestTools.GetString("mobile").Trim() != "" &&
                !Regex.IsMatch(RequestTools.GetString("mobile").Trim(), @"^[\d|-]+$"))
            {
                AddErrLine("移动电话号码中含有非法字符");
            }

            if (RequestTools.GetString("phone").Trim() != "" &&
                !Regex.IsMatch(RequestTools.GetString("phone").Trim(), @"^[\d|-]+$"))
            {
                AddErrLine("固定电话号码中含有非法字符");
            }


            if (RequestTools.GetString("description").Length > 500)
            {
                //如果自我介绍超过500...
                AddErrLine("自我介绍不得超过500个字符");
                return;
            }
            if (RequestTools.GetString("interest").Length > 50)
            {
                //如果兴趣超过50...
                AddErrLine("兴趣介绍不得超过50个字符");
                return;
            }

            UserInfo __userinfo = new UserInfo();

            __userinfo.Gender = RequestTools.GetString("sex");
            __userinfo.MobilePhone = RequestTools.GetString("mobile");
            __userinfo.Telephone = RequestTools.GetString("phone");
            __userinfo.ZipCode = RequestTools.GetString("zip");

            string birthday = RequestTools.GetFormString("birthday");
            __userinfo.Birthday = Convert.ToDateTime(birthday);

            //__userinfo.Birthday.Year = RequestTools.GetFormInt("year", 1980);
            //__userinfo.Birthday.Month = RequestTools.GetFormInt("month", 1);
            //__userinfo.Birthday.Day = RequestTools.GetFormInt("day", 1);

            __userinfo.Id = UserData.Id;

            __userinfo.ProvinceId = RequestTools.GetFormInt("province",-1);
            __userinfo.CityId = RequestTools.GetFormInt("city",-1);


            __userinfo.Interests = Utils.HtmlEncode(RequestTools.GetString("interest"));

            __userinfo.Description = Utils.HtmlEncode(RequestTools.GetString("description"));

            UserBLL.Instance.UpdateUserProfile(__userinfo);
            

            SetUrl("BasicInfo.aspx");
            SetMetaRefresh();
            SetShowBackLink(true);
            AddMsgLine("修改个人信息完毕");
        }
    }
}
